package com.zhentao.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhentao.pojo.RepairShops;
import com.zhentao.pojo.ShopCertifications;
import com.zhentao.pojo.ShopServiceOfferings;
import com.zhentao.service.*;
import com.zhentao.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/shop")
public class ShopController {

    @Autowired
    private RepairShopsService shopService;

    @Autowired
    private ShopServiceOfferingsService serviceService;
    @Autowired
    private ShopBusinessHoursService shopBusinessHoursService;
    @Autowired
    private ServicesService servicesService;

    @Autowired
    private ShopCertificationsService certificationService;

    // 门店管理
    @RequestMapping("/list")
    public Result getShopList(@RequestBody com.zhentao.pojo.Page page) {
        Page<RepairShops> page1=new Page<>(page.getPageNum(), page.getPageSize());
        QueryWrapper<RepairShops> queryWrapper = new QueryWrapper<>();
        Page<RepairShops> list = shopService.page(page1,queryWrapper);
        return Result.OK(list);
    }

    @RequestMapping("/add")
    public Result  addShop(@RequestBody RepairShops shop) {
        shopService.save(shop);
        return Result.OK();
    }

    @RequestMapping("/update")
    public Result  updateShop(@RequestBody RepairShops shop) {
        shopService.updateById(shop);
        return Result.OK();
    }

    @RequestMapping("/delete/{id}")
    public Result  deleteShop(@PathVariable Long id) {
        shopService.removeById(id);
        return Result.OK();
    }

    // 服务项目管理
    @RequestMapping("/service/list")
    public Result getServiceList(@RequestBody com.zhentao.pojo.Page page) {
        Page<ShopServiceOfferings> page1 = new Page<>(page.getPageNum(), page.getPageSize());
        QueryWrapper<ShopServiceOfferings> queryWrapper = new QueryWrapper<>();
        Page<ShopServiceOfferings> list = serviceService.page(page1,queryWrapper);
        for (ShopServiceOfferings service : list.getRecords()){
            service.setShopBusinessHours(shopBusinessHoursService.getById(service.getShopId()));
            service.setServices(servicesService.getById(service.getServiceId()));
        }
        return Result.OK(list);
    }

    @RequestMapping("/service/add")
    public Result addService(@RequestBody ShopServiceOfferings service) {
        serviceService.save(service);
        return Result.OK();
    }

    @RequestMapping("/service/update")
    public Result updateService(@RequestBody ShopServiceOfferings service) {
        serviceService.updateById(service);
        return Result.OK();
    }

    @RequestMapping("/service/delete/{id}")
    public Result deleteService(@PathVariable Long id) {
        serviceService.removeById(id);
        return Result.OK();
    }

    // 门店认证管理
    @RequestMapping("/certification/list")
    public Result getCertificationList(@RequestBody com.zhentao.pojo.Page page) {
        Page<ShopCertifications> page1 = new Page<>(page.getPageNum(), page.getPageSize());
        QueryWrapper<ShopCertifications> queryWrapper = new QueryWrapper<>();
        Page<ShopCertifications> list = certificationService.page(page1,queryWrapper);
        return Result.OK(list);
    }

    @RequestMapping("/certification/approve")
    public Result approveCertification(@RequestBody ShopCertifications certification) {
        certification.setStatus(1);
        certificationService.updateById(certification);
        return Result.OK();
    }

    @RequestMapping("/certification/reject")
    public Result rejectCertification(@RequestBody ShopCertifications certification) {
        certification.setStatus(0);
        certificationService.updateById(certification);
        return Result.OK();
    }
}
